package com.bawei.week.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.lib_base.entity.ParkBeanItem
import com.bawei.week.R
import com.bawei.week.databinding.ItemParkBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class ParkAdapter : PagingDataAdapter<ParkBeanItem,ParkAdapter.ParkViewHolder>(diff){
    companion object{
        val diff = object : DiffUtil.ItemCallback<ParkBeanItem>() {
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id == newItem.id
            }
        }
    }

    private var mOnItemClickListener : OnItemClickListener ? = null

    private var mOnItemLongClickListener : OnItemLongClickListener ? = null

    class ParkViewHolder(itemView: View) : ViewHolder(itemView){
        val binding : ItemParkBinding?= DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkAdapter.ParkViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding?.apply {
            parkSpaceNumber.text = item.spaceNumber
            parkAreaName.text = "所属区域:${item.areaName}"
            if (item.licensePlate.isNullOrEmpty()){
                parkLicensePlate.visibility = View.GONE
            }else{
                parkLicensePlate.text = "车牌号:${item.licensePlate}"
            }
            if (item.userId == 0 || item.userId.toString().isEmpty()){
                parkUserName.visibility = View.GONE
            }else{
                parkUserName.text = "使用人:${item.userName}"
            }
            parkSpaceStatus.text = item.spaceStatus
            if (item.spaceStatus == "空闲"){
                parkSpaceStatus.setBackgroundResource(R.drawable.shape_spare)
                parkSpaceStatus.setTextColor(Color.GREEN)
            }else if (item.spaceStatus == "已占用"){
                parkSpaceStatus.setBackgroundResource(R.drawable.shape_occupied)
                parkSpaceStatus.setTextColor(Color.BLUE)
            }else if (item.spaceStatus == "维护中"){
                parkSpaceStatus.setBackgroundResource(R.drawable.shape_under_repair)
                parkSpaceStatus.setTextColor(Color.DKGRAY)
            }
        }

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position,item)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemClick(item)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkAdapter.ParkViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(inflate)
    }

    fun setOnItemChildClick(listener: OnItemClickListener?){
        this.mOnItemClickListener = listener
    }

    fun setOnItemChildLongClick(listener: OnItemLongClickListener?){
        this.mOnItemLongClickListener = listener
    }
}

interface OnItemClickListener{
    fun onItemClick(position: Int,bean : ParkBeanItem)
}

interface OnItemLongClickListener{
    fun onItemClick(bean: ParkBeanItem)
}